package frame.m.com.mylibrary.http;

import android.content.Context;

import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.observers.DisposableObserver;

/**
 * Created by Administrator on 2017/7/7.
 */

public abstract class ProgressSubscriber<T> extends DisposableObserver<T> implements ProgressCancelListener{

    private LoaddingDialogHandler dialogHandler;


    public ProgressSubscriber(Context context)
    {
        dialogHandler=new LoaddingDialogHandler(context,this,true);
    }


    public void showProgressDialog(){
        if(dialogHandler!=null)
        {
            dialogHandler.show();
        }
    }

    public void dismissProgressDialog(){
        if(dialogHandler!=null)
        {
            dialogHandler.dismiss();
            dialogHandler=null;
        }
    }


    @Override
    public void onCancelProgress() {
//        if(!this.i)
    }

    @Override
    public void onNext(T t) {
        _onNext(t);
    }

    @Override
    public void onError(Throwable t) {
        t.printStackTrace();
        //判断是否是网络异常
        if(false){
            _onError(new ApiException("请求失败,请稍后重试..."));
        }else if (t instanceof ApiException){
            _onError((ApiException)t);
        }else
        {
            _onError(new ApiException("请求失败,请稍后重试..."));
        }
        dismissProgressDialog();
    }

    @Override
    public void onComplete() {
        dismissProgressDialog();
    }


    protected  abstract void _onNext(T t);
    protected  void _onError(ApiException t){
        ToastUtils.showShort(t.getMessage());
    }
}
